package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.POJO.ArrangeDiary;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface ArrangeDiaryMapper {

    /**
     * 分页查询随访日记列表数据(模糊查询)
     * @param queryString
     * @return
     */
    @Select("<script>" +
            "SELECT a.id,a.visitPeople,a.returnDate,a.method,a.visitContext,a.feedback,a.resultCode,m.name beIntervenePeople \n" +
            "FROM `t_arrangediary` a LEFT JOIN `t_member` m \n" +
            "ON a.member_id = m.id" +
            "<where>\n" +
            "            <if test=\"value != null and value.length > 0\">\n" +
            "                and m.name like\n" +
            "                \"%\"#{value}\"%\" or m.fileNumber like \"%\"#{value}\"%\" \n" +
            "            </if>\n" +
            "        </where>" +
            "</script>")
    Page<Map> pageQuery(String queryString);


    /**
     * 新增随访笔记
     * @param arrangeDiary
     */
    @Insert("insert into t_arrangediary" +
            "(id,returnDate,method,visitContext,feedback,resultCode,theme,originalFilename,fileName,visitPeople,member_id) " +
            "values(null,#{returnDate},#{method},#{visitContext},#{feedback},#{resultCode},#{theme},#{originalFilename},#{fileName},#{visitPeople},#{memberId})")
    void addArrangeDiary(ArrangeDiary arrangeDiary);

    /**
     * 根据id删除随访日记信息
     * @param id
     */
    @Delete("delete from t_arrangediary where id = #{id}")
    void deleteArrangeDiary(Integer id);


    /**
     * 根据id查询随访日记信息
     * @param id
     * @return
     */
    @Select("SELECT a.id,a.visitPeople,a.returnDate,a.method,a.visitContext,a.feedback,a.theme,a.originalFilename,a.fileName,a.resultCode,m.fileNumber,m.name beIntervenePeople  " +
            " FROM `t_arrangediary` a LEFT JOIN `t_member` m " +
            " ON a.member_id = m.id  " +
            " where a.id=#{id} ")
    Map<String,Object> findArrangeDiaryById(Integer id);


    /**
     * 根据id查询数据文件存档名
     * @param id
     * @return
     */
    @Select("select fileName from t_arrangediary where id = #{id}")
    String findFileNameById(Integer id);

    /**
     * 修改随访日记信息
     * @param arrangeDiary
     */
    @Update("<script>" +
            " update t_arrangediary \n" +
            "        <set>\n" +
            "            <if test=\"returnDate != null\">\n" +
            "                returnDate = #{returnDate},\n" +
            "            </if>\n" +
            "            <if test=\"method != null\">\n" +
            "                method = #{method},\n" +
            "            </if>\n" +
            "            <if test=\"visitContext != null\">\n" +
            "                visitContext = #{visitContext},\n" +
            "            </if>\n" +
            "            <if test=\"feedback != null\">\n" +
            "                feedback = #{feedback},\n" +
            "            </if>\n" +
            "            <if test=\"resultCode != null\">\n" +
            "                resultCode = #{resultCode},\n" +
            "            </if>\n" +
            "            <if test=\"visitPeople != null\">\n" +
            "                visitPeople = #{visitPeople},\n" +
            "            </if>\n" +
            "            <if test=\"theme != null\">\n" +
            "                theme = #{theme},\n" +
            "            </if>\n" +
            "            <if test=\"originalFilename != null\">\n" +
            "                originalFilename = #{originalFilename},\n" +
            "            </if>\n" +
            "            <if test=\"fileName != null\">\n" +
            "                fileName = #{fileName},\n" +
            "            </if>\n" +
            "        </set>\n" +
            "        where id = #{id}" +
            "</script>")
    void editArrangeDiary(ArrangeDiary arrangeDiary);

}
